var hasInitNumberBox = false;
var gNumberValue = '';
var gNumberList ;
var numberTextList;
var lastNumberKeyA;
function pressKey(num){
	if(!hasInitNumberBox){
		initNumberBox();
	}
	lastNumberKeyA = document.activeElement;
	
	gNumberValue = ''+num;
	
	var numberBox = document.getElementById('numberBox');
	numberBox.style.display = 'block';
	changeNumberValue();
	
	var bgodyBox = document.getElementById('bgodyBox');
	if(bgodyBox){
		bgodyBox.style.display = 'none';
	}
	
	var date = new Date();
	KEY.PRESSTIME = date.getTime();		//
	if(this.hideNumberKeyTimer) {clearInterval(this.hideNumberKeyTimer);this.hideNumberKeyTimer = null;}
	this.hideNumberKeyTimer = setInterval( "hideNumberKeyAuto()",1*1000);	
}

function hideNumberKeyAuto(){
	var date = new Date();
	var curTime = date.getTime();	
	if((curTime - KEY.PRESSTIME) >= 5000){
		if(this.hideNumberKeyTimer) {clearInterval(this.hideNumberKeyTimer);this.hideNumberKeyTimer = null;}
		if(gNumberList.length == 1){
			hideNumberKeyBox();
			onclickNumberItem(gNumberList[0]);
		}else{
			hideNumberKeyBox();
		}
	}
}

function hideNumberKeyBox(){
	KEY.CURTYPE= false;
	var bgodyBox = document.getElementById('bgodyBox');
	if(bgodyBox){
		bgodyBox.style.display = 'block';
	}
	if(lastNumberKeyA){
		lastNumberKeyA.focus();
	}
	var numberBox = document.getElementById('numberBox');
	numberBox.style.display = 'none';
	
	if(this.hideNumberKeyTimer) {clearInterval(this.hideNumberKeyTimer);this.hideNumberKeyTimer = null;}
}

function initNumberBox(){
	var numberKeyA = document.getElementById('numberKeyA');
	numberKeyA.onfocus = focusNumberKeyValue;
	numberKeyA.onblur = blurNumberKeyValue;
	numberKeyA.onclick = clickNumberKeyValue;
	
	numberTextList = new TextList({
		target:'numberKey',
		disNum:9,
		itemHeight:54,
		content:'pNumberListBox',
		focusClass:'_numberListCurItem',
		normalClass:'_numberListItem',
		emptyClass:'_numberListItem0',
		splitBar:'pNumberListSplitBar',
		splitHeight:486,
		formatHtml:formatNumberKeyHtml,
		onfocus:onfocusNumberItem,
		onclick:onclickNumberItem,
	});
}

function delNumberValue(){
	if(gNumberValue.length > 0){
		gNumberValue = gNumberValue.substr(0,gNumberValue.length - 1);
		changeNumberValue();
	}
	
}
function addNumberValue(n){
	if(gNumberValue.length >= 3){
		gNumberValue = ''+n;
	}else{
		gNumberValue += n;
	}
	changeNumberValue();
}


function focusNumberKeyValue(event){
	var a = event.currentTarget;
	var par = a.parentNode.parentNode;
	par.className = '_numberListCurItem';
	
	KEY.CURTYPE='numberKey';
}

function blurNumberKeyValue(event){
	var a = event.currentTarget;
	var par = a.parentNode.parentNode;
	par.className = '_numberListItem';
}

function clickNumberKeyValue(event){
	var a = event.currentTarget;
	if(gNumberList.length > 0){
		hideNumberKeyBox();
		onclickNumberItem(gNumberList[0]);
	}
}

function formatNumberKeyHtml(data,id){
	var con = '';
	
	con += '<div class="_numberListNum">'+getDisNumberKey(data)+'</div>';
	con += '<div class="_numberListText"><a href="#" id="'+this.target+'-a-'+id+'">'+data.name+'</a></div>';
	
	return con;
}
function onfocusNumberItem(data,tar){
	KEY.CURTYPE='numberKey';
}
function onclickNumberItem(data){
	gotoPlayPage(data,data.arrayId,1);
}


function changeNumberValue(){
	document.focus('numberKeyA');
	
	var numberKeyA = document.getElementById('numberKeyA');
	numberKeyA.innerHTML = gNumberValue+'&nbsp;';
	gNumberList = findNumberList();
	
	
	numberTextList.setDataList(gNumberList,false);
	
	var numberKeyValue = document.getElementById('numberKeyValue');
	if(gNumberList.length > 0){
		numberKeyValue.innerHTML = gNumberList[0].name;
	}else{
		numberKeyValue.innerHTML = '&nbsp;';
	}
	if(gNumberList.length== 1){
		var numberKeyA = document.getElementById('numberKeyA');
		numberKeyA.click();
	}
	document.refresh();
}



function findNumberList(){
	var list = new Array();
	if(gNumberValue){
		for(var i = 0;i<gDataList.length;i++){
			var data = gDataList[i];
			var dis = getDisNumberKey(data);
			if(dis.indexOf(gNumberValue) >= 0){
				list.push(data);
			}
		}
	}
	list.sort(descByNumber );
	return list;
}

function getDisNumberKey(data){
	var dis = data.displayId;
	var a = parseInt(dis/100);
	dis = dis%100;
	var b = parseInt(dis/10);
	var c = dis%10;
	
	return a+''+b+''+c;
}

KEY.TYPE['numberKey'] = function(key){
	if(key == 0x11){
		hideNumberKeyBox();
		return false;
	}else if(key == 0xC){		//left
		delNumberValue();
		return false;
	}else if(key >= 0x0 && key <= 0x9){
		addNumberValue(key);
	}
	return true;	
}